package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavor = dishDto.getFlavors();
        /*flavor.stream().map((item)->{
           item.setDishId(dishId);
           return item;
        }).collect(Collectors.toList());*/
        for (DishFlavor dishFlavor : flavor) {
            dishFlavor.setDishId(dishId);
        }
        //保存菜品口味的基本信息到菜品表dish_flavor
        dishFlavorService.saveBatch(flavor);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息,从dish表查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> dishFlavor = new LambdaQueryWrapper<>();
        dishFlavor.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(dishFlavor);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 修改菜品信息和对应的口味信息
     * */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据--dish_flavor表中的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据--dish_flavor表中的insert操作
        List<DishFlavor> list = dishDto.getFlavors();
        for (DishFlavor dishFlavor : list) {
            dishFlavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(list);
    }

    /**
     * 删除菜品及对应的口味信息
     * */
    @Transactional
    @Override
    public void deletearr(String ids) {
        String [] arr = ids.split(",");
        for (String s : arr) {
            LambdaQueryWrapper<DishFlavor> queryWrapper =new LambdaQueryWrapper<>();
            dishService.removeById(s);
            queryWrapper.eq(DishFlavor::getDishId,s);
            dishFlavorService.remove(queryWrapper);
        }

    }











}
